package algorithm.leetcode;

import java.util.Arrays;

/**
 * @author SeonExlike
 * @since 2024/3/10
 */
public class NO26_删除有序数组中的重复项 {

    public static void main(String[] args) {
        new SolutionGrace().removeDuplicates(new int[]{1, 1, 2});
    }

    static class SolutionViolence {
        public int removeDuplicates(int[] nums) {
            int[] handle = Arrays.stream(nums).distinct().toArray();
            System.arraycopy(handle, 0, nums, 0, handle.length);
            return handle.length;
        }
    }

    static class SolutionGrace {
        public int removeDuplicates(int[] nums) {
            if (nums.length == 0) {
                return 0;
            }
            int slow = 0, fast = 0;
            while (fast < nums.length) {
                if (nums[fast] == nums[slow]) {
                    fast++;
                } else {
                    nums[++slow] = nums[fast];
                }
            }
            return ++slow;
        }
    }
}
